const app = getApp();
var timer = false;
var QQMapWX = require('../../lib/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    page: 1,
    pois: [],
    result: [],
    latitude: '',
    longitude: '',
    circle:[],//画个圆形。。。
    markers: [
      {
        id: 0,
        iconPath: "/images/address.png",
        latitude: 34.28594472914285,
        longitude: 108.07340294122699,
        width: 20,
        height: 20
      },
      {
        id: 1,
        iconPath: "/images/address.png",
        latitude: 34.28345098172088,
        longitude: 108.07423643767835,
        width: 20,
        height: 20
      },
      {
        id: 1,
        iconPath: "/images/address.png",
        latitude: 34.28520896777005,
        longitude: 108.0694815516472,
        width: 20,
        height: 20
      },
      {
        id: 2,
        iconPath: "/images/address.png",
        latitude: 34.2842427171466,
        longitude: 108.0724158883095,
        width: 20,
        height: 20
      },
      {
        id: 1,
        iconPath: "/images/address.png",
        latitude: 34.286067170734036,
        longitude: 108.0664473026991,
        width: 20,
        height: 20
      },
      {
        id: 1,
        iconPath: "/images/address.png",
        latitude: 34.287375788724745,
        longitude: 108.06752823293209,
        width: 20,
        height: 20
      },
      {
        id: 1,
        iconPath: "/images/address.png",
        latitude: 34.28801845563627,
        longitude: 108.07408086955549,
        width: 20,
        height: 20
      },
      {
        id: 1,
        iconPath: "/images/address.png",
        latitude: 34.288367488192435,
        longitude: 108.07558692991735,
        width: 20,
        height: 20
      },
      {
        id: 1,
        iconPath: "/images/address.png",
        latitude: 34.28487044889044,
        longitude: 108.07326279580593,
        width: 20,
        height: 20
      },
      {
        id: 1,
        iconPath: "/images/address.png",
        latitude: 34.28487044889044,
        longitude: 108.07326279580593,
        width: 20,
        height: 20
      }]

  },

  onLoad: function (opentions) {
    this.setData({
      address:opentions.address,
      latitude:opentions.lat,
      longitude:opentions.lng
    })
    let vm = this;
   
    qqmapsdk = new QQMapWX({
      key: '64CBZ-OA5K4-KH3UV-DMCQX-2P4MJ-VJFQD'
    })
    vm.getLocal(Number(opentions.lat), Number(opentions.lng))
  },
  onShow: function () {
  
   // vm.getUserLocation();
  },
  // 授权位置信息
  getUserLocation: function () {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation'] != undefined &&
          res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] = true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        }
        else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },
  // 微信获得经纬度
  getLocation: function () {
    let vm = this;
  
  },
  // 获取当前地理位置
  getLocal: function (latitude, longitude) {
    let that = this;
    wx.showLoading({
      title: '加载中',
      duration: 3000
    })
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude,
      },
      success: function (params) {
        console.log('params', params)
        that.setData({
          result: params.result
        })
      }
    }),
      qqmapsdk.search({
        location: {
          latitude: latitude,
          longitude: longitude,
        },
        keyword: '停车场',
        page_size: 10,
        page_index: that.data.page,
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          wx.hideLoading()
          let pois = res.data;
          console.log("停车场位置信息：",pois);
          let Longest_distance = 0;//得到一个无聊的最大距离，用来画圆形
          for (var i = 0; i < res.data.length; i++) {
            that.data.markers[i].latitude = res.data[i].location.lat;
            that.data.markers[i].longitude = res.data[i].location.lng;
            //也为这些标记点绘制一些label
            that.data.markers[i].label = {
              content:res.data[i].title,
              color:'#002329',
              fontSize:10,
              borderWidth:2,
              borderColor:'#10239e',
              borderRadius:5,
              bgColor:'#e6f7ff',
              padding:1,
              textAlign:"center",
            }

            //得到所有停车场中距离最远的。。。
            if(Longest_distance < res.data[i]._distance)Longest_distance = res.data[i]._distance;

          }
          //把自身中心点也加上去：
          let center = {
            id: 99,
            iconPath: "/images/caddress.png",
            latitude: that.data.latitude,
            longitude: that.data.longitude,
            width: 20,
            height: 20
          }
          that.data.markers.push(center);

          //无聊画个圆形
          let _circle = {
            latitude: that.data.latitude,
            longitude: that.data.longitude,
            color: '#FF0000DD',
            fillColor: '#7cb5ec88',
            radius: Longest_distance,
            strokeWidth: 2
          };
          that.data.circle.push(_circle);
          console.log("地图上面绘制圆形：",that.data.circle);
          //刷新界面
          that.setData({
            markers: that.data.markers,
            circle: that.data.circle
          })
          // console.log("pois:"+JSON.stringify(pois))
          that.setData({
            pois: that.data.pois.concat(pois),
          })
        },
        fail: function (res) {
          console.log(res);
        },
        complete: function (res) {
          // console.log(res);
        }
      });

  },

  call:function(e){
    var tel= e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: tel,
    })
  },

  // 点击进入地图
  turn_map: function (e) {
    console.log('e', e)
    wx.openLocation({
      latitude: e.currentTarget.dataset.index.lat,
      longitude: e.currentTarget.dataset.index.lng,
      scale: 18,
      name: e.currentTarget.dataset.name,
      address: e.currentTarget.dataset.address,
    })
    console.log('e', e)
  },

  tosenic: function () {
    var that = this;
    wx.openLocation({
      latitude: parseInt(that.data.park.latitude),
      longitude: parseInt(that.data.park.longitude),
      scale: 18,
      name: '',
      address: that.data.park.name
    })
  },

  //怎么说呢，就是查询了之后，就有了相关的信息，点进这个按钮，将相关信息作为参数，调用wx.openLocation()这个方法，直接调用第三方的地图软件进行导航，为什么不使用腾讯的路线规划插件我个人感觉是因为同一个接口用得太多就没意思了。
  routePlan(e) {
    let that = this;
    let lat = e.currentTarget.dataset.lat;
    let lng = e.currentTarget.dataset.lng;
    let title = e.currentTarget.dataset.title;
    wx.getLocation({ //获取当前经纬度
        type: 'wgs84', //返回可以用于wx.openLocation的经纬度，
        success: function (res) {
          wx.openLocation({ //​使用微信内置地图查看位置。
            latitude:lat, //要去的纬度-地址
            longitude: lng, //要去的经度-地址
             name: title,
            address: title
          })
        }
      })
  },
  //原本
  callRoutePlanPlugin(lat,lng,title) {

    var that = this;
    console.log("lat:"+lat+" lng:"+lng+"title:"+title)
    let key = '64CBZ-OA5K4-KH3UV-DMCQX-2P4MJ-VJFQD';  //使用在腾讯位置服务申请的key
    let referer = '驾车出行';   //调用插件的app的名称
    let endPoint = JSON.stringify({  //终点
      'name': title,
      'latitude': lat,
      'longitude': lng
    });
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
    });
  },


  // 下拉获取数据页数
  onReachBottom: function () {
    let vm = this;
    vm.setData({
      page: vm.data.page + 1
    })
    vm.getLocal(vm.data.latitude, vm.data.longitude)
  }
})